﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
/// <summary>
/// 商店管理脚本
/// </summary>
public class ShopModel : MonoBehaviour
{
    //public UIInput PayInput;//输入充值手机号
    //public UILabel CostGold;//兑换物品需要的金币
    //public UILabel Cost;//兑换物品获得的话费
    //public UISprite Voucher;//兑换物品的图片
    //按钮    
    public GameObject CloseBtn;//关闭商店界面
    public GameObject ShopPanel;

    public List<GoodsItem> listGood = new List<GoodsItem>();

    public List<ShopItem> listItem = new List<ShopItem>();

    public GameObject Grid;

    public UIGrid uIGrid;

    EginUser user = EginUser.Instance;
    //public List<Coin2PhoneItem> items = new List<Coin2PhoneItem>();
    //public List<ShopItem> redPacketItems;
    private void Awake()
    {
        UIEventListener.Get(CloseBtn).onClick = ButtonClick;

        uIGrid = Grid.GetComponent<UIGrid>();
    }
    void Start()
    {
        LogMsg();
    }
    void ButtonClick(GameObject button)
    {
        if (button.name == "CloseBtn")
        {
            Destroy(ShopPanel);
        }
    }

    public void LogMsg()
    {
        if (listGood.Count > 0)
        {
            int length = listGood.Count;
            for (int i = 0; i < length; i++)
            {
                GameObject item = Instantiate(Resources.Load(HallPrafabPath.ShopItem), Grid.transform) as GameObject;
                item.name = "item";

                uIGrid.repositionNow = true;
                ShopItem shopItem = item.GetComponent<ShopItem>();
                if (shopItem)
                {
                    shopItem.Title.text = listGood[i].Title;
                    shopItem.tempID = listGood[i].Id;
                    shopItem.coinCountLabel.text = user.ReturnMoney(listGood[i].Price);
                    shopItem.textureUrl = listGood[i].ImgUrl;
                    StartCoroutine(GetWWWFACE(listGood[i].ImgUrl, shopItem.texture));
                }
            }
        }
    }
    int width, height;
    IEnumerator GetWWWFACE(string url, GameObject go)
    {
        if (url != "")
        {
            WWW www = new WWW(url);
            yield return www;
            if (www.isDone)
            {

                if (www.error == null) { }
                //Avatar.mainTexture = www.texture;
                print("商店图标下载地址是：" + url);
                Texture2D texture2D = www.texture;
                go.AddComponent<UITexture>().mainTexture = www.texture;

                width = www.texture.width;
                height = www.texture.height;

                int[] ints = Set(width, height);

                go.GetComponent<UITexture>().width = ints[0];
                go.GetComponent<UITexture>().height = ints[1];

            }
            else
            {

            }
            www.Dispose();
        }
    }
    public int[] Set(int width, int height)
    {
        int[] ints = new int[2];

        float wid = width;
        float hei = height;

        float bili = wid / hei;

        bili = Mathf.Abs(bili);

        //ints[0] = 80;
        //ints[1] = Mathf.CeilToInt(ints[0] / bili);

        ints[1] = 120;
        ints[0] = Mathf.CeilToInt(ints[1] * bili);

        return ints;
    }
}
